// package-market/pages/lottery-detail/index.js
const app = getApp();
import { PACKAGE_MARKET_PAGE } from '../../../constants/route';
import { ACTIVITY_TYPE } from '../../../constants/common';
import { ACTIVITY_STATUS, ACTIVITY_STATUS_VALUE } from '../../../constants/activity';
import { navToAction, showLoading, hideLoading } from '../../../common/env';
import { UserInfoUpdateMixins, ShareImageMixins, PageCommonFuncs, WxpayApplymentTipMixins } from '../../../behaviors/page';
import { getActivityInfo } from '../../../api/usr';
let loaded = false, activityId, shareUserId, showSuccess;

Page({
    mixins: [UserInfoUpdateMixins, ShareImageMixins, PageCommonFuncs, WxpayApplymentTipMixins],
    data: {
        ACTIVITY_TYPE,
        ACTIVITY_STATUS_VALUE,
        ACTIVITY_STATUS,
        activityInfo: {},
        successModal: {
            show: false,
        },
    },
    onLoad(options) {
        shareUserId = options.b || '';
        activityId = options.activityId;
        showSuccess = options.showSuccess;
    },
    onShow() {
        if (!app.globalData.isReadyAuth) {
            const _ = this;
            const originFunc = app.userIdentifyCallback;
            app.userIdentifyCallback = function (...args) {
                console.log('执行认证后回调函数, onShow');
                _.onShow(...args);
                return originFunc && originFunc(...args);
            }
            return;
        } else {
            this._handleGetDetail();
        }
    },
    async _handleGetDetail() {
        !loaded && showLoading('加载中...');
        const activityInfo = await getActivityInfo(activityId);
        console.log('LotteryDetail => 抽奖活动详情为', activityInfo);
        this.setData({
            activityInfo,
            'successModal.show': !!showSuccess,
        });
        if (!loaded) {
            let { brandId, isWxpayAvailable } = activityInfo;
            this.handleShowWxpayTip({ brandId, isWxpayAvailable });
        }
        showSuccess = false;
        if (!loaded) hideLoading(); loaded = true;
    },
    handleNavToLottery() {
        const { activityInfo } = this.data;
        navToAction(PACKAGE_MARKET_PAGE['lotteryDraw'], {
            activityId,
        }, {
            success(res) {
                setTimeout(() => {
                    app.event.trigger('sendLotteryActivityInfoForDraw', activityInfo);
                }, 800);
            },
        });
    },
})
